<?php

$XML = new DOMDocument();
$XML->load("Sport.xml");
$xpath = new DOMXPath($XML);

$d = dir("C:/bit/web/ponuda/public_html/test");
while (false !== ($entry = $d->read()))
{
  if(strpos($entry, "leaguetable")!==FALSE)
  {echo "*$entry.\n";
  $XMLfra = new DOMDocument();
  $XMLfra->load($entry);
  $xpathFra = new DOMXPath($XMLfra);
  $xCatSP = $xpathFra->query("//ns2:SportradarData/Sport/@id");
  $xCat = $xpathFra->query("//ns2:SportradarData/Sport/Category");
  $x = $xpath->query("//ns2:SportradarData/Sport[@id='" . $xCatSP->item(0)->textContent . "']");

  $xNew = $XML->importNode($xCat->item(0), true);
  $x->item(0)->appendChild($xNew);
  $XML->save("Sport_generated.xml");
  }
}
$d->close();
die("kraj");
